package com.sc.service.Vast_service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.Vast_pojo.domain.DeviceFix;
import com.sc.Vast_pojo.domain.PublicDevice;
import com.sc.Vast_pojo.dto.*;
import com.sc.Vast_pojo.vo.PublicDeviceVO;
import com.sc.mapper.Vast_Mapper.DeviceFixMapper;
import com.sc.mapper.Vast_Mapper.DeviceMapper;
import com.sc.result.PageResult;
import com.sc.service.Vast_service.DeviceFixService;
import com.sc.service.Vast_service.PublicDeviceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Vast
 * @version 1.0
 * @function:公共设备管理服务层实现类
 */
@Service
public class PublicDeviceServiceImpl extends ServiceImpl<DeviceMapper,PublicDevice> implements PublicDeviceService {
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private DeviceFixMapper deviceFixMapper;
    @Autowired
    private DeviceFixService deviceFixService;
    @Override
    //TODO 设置添加人需要查询当前的登录用户
    public void save(PublicDeviceDTO publicDeviceDTO) {
        PublicDevice publicDevice = new PublicDevice();
        BeanUtils.copyProperties(publicDeviceDTO,publicDevice);
        publicDevice.setAdder("王七");
        publicDevice.setAddTime(LocalDateTime.now());
        deviceMapper.insert(publicDevice);
    }

    /**
     * 根据主键id更新公共设备信息
     * @param publicDeviceDTO
     * @param id
     */
    @Override
    //TODO 设置添加人需要查询当前的登录用户
    public void update(PublicDeviceDTO publicDeviceDTO, Integer id) {
        PublicDevice publicDevice = new PublicDevice();
        BeanUtils.copyProperties(publicDeviceDTO,publicDevice);
        publicDevice.setAdder("王七");
        publicDevice.setAddTime(LocalDateTime.now());
        QueryWrapper<PublicDevice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        deviceMapper.update(publicDevice,queryWrapper);
    }

    /**
     * 公共设备删除
     * @param ids
     */
    @Override
    public void delete(List<Integer> ids) {
        deviceMapper.deleteBatchIds(ids);
    }

    /**
     * 查询设备详情,并且显示出对应的维修记录
     * @param id
     * @return
     */
    @Override
    public PublicDeviceVO getById(Integer id) {
        PublicDeviceVO publicDeviceVO = new PublicDeviceVO();
        PublicDevice publicDevice = deviceMapper.selectById(id);
        BeanUtils.copyProperties(publicDevice,publicDeviceVO);
        //获取当前公共设备的设备编号
        String deviceNum = publicDevice.getDeviceNum();

        //设置参数
        Page<DeviceFix> page=new Page<>(1,5);
        QueryWrapper<DeviceFix> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fix_num",deviceNum);
        deviceFixService.page(page,queryWrapper);
        List<DeviceFix> records = page.getRecords();
        publicDeviceVO.setDeviceFixList(records);
        return publicDeviceVO;
    }

    /**
     * 公共设备管理分页查询
     * @param publicDevicePageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(PublicDevicePageQueryDTO publicDevicePageQueryDTO) {
        Page<PublicDevice> page = new Page<>(publicDevicePageQueryDTO.getPage(), publicDevicePageQueryDTO.getPageSize());
        QueryWrapper<PublicDevice> queryWrapper = new QueryWrapper<>();
        String group = publicDevicePageQueryDTO.getGroup();
        String deviceNum = publicDevicePageQueryDTO.getDeviceNum();
        String name = publicDevicePageQueryDTO.getName();
        if (group != null && !group.trim().isEmpty()) {
            queryWrapper.eq("`group`",group);
        }
        //任选其一
        if (deviceNum != null && !deviceNum.trim().isEmpty()) {
            queryWrapper.eq("device_num",deviceNum);
        }else if(name != null && !name.trim().isEmpty()) {
            queryWrapper.eq("`name`",name);
        }
        Page<PublicDevice> res = page(page, queryWrapper);

        long total=res.getTotal();
        List<PublicDevice> records = res.getRecords();

        return new PageResult(total,records);
    }
}
